function setOutput(txt, error) {
    var output, e, next;

    output = document.querySelector('#output');
    for (e = output.firstChild; e; e = next) {
        next = e.nextSibling;
        output.removeChild(e);
    }

    output.appendChild(document.createTextNode(txt));

    if (error) {
        output.classList.add('error');
    } else {
        output.classList.remove('error');
    }
}

// instantiate a worker to run tests in background
w=new Worker("web_worker.js");

setOutput('Running...');

// add message handler
w.onmessage = function (event) {
    var download;
    if (event.data.type === 'progress') {
        setOutput(event.data.msg);
    } else {
        setOutput(event.data.format);
        download = document.querySelector('#download');
        download.style.display = '';
        download.href = 'data:application/json,' + encodeURIComponent(JSON.stringify(event.data.data));
    }
};

w.onerror = function (event) {
    setOutput(event.message, true);
};
